<?php namespace models;
/**
 * This class is property of Piotr Poradzisz. All rights reserved. DO NOT COPY!
 * @author Piotr Poradzisz
 * email: piotr.poradzisz@gmail.com
 * tel.: +48668027020
 */

class Image {

    private $ext;

    public $name;

    function __construct($file, $source){
        $this->file = $file;
        $this->source = $source;
    }

    public function resize($sizes, $newName, $newPath) {
        foreach($sizes as $sizeKey => $size){
            $imageName = $newName."_$sizeKey";
            $this->setImagesSize($size);
            echo "$this->imageSizeX, $this->imageSizeY, $this->source/$this->file, $newPath/$imageName.$this->ext, $this->ext";
            $this->imageResize($this->imageSizeX, $this->imageSizeY, "$this->source/$this->file", "$newPath/$imageName.$this->ext", $this->ext);
            $this->size[$sizeKey] = getimagesize("$newPath/$imageName.$this->ext");
        }
    }

    private function setImagesSize($sizeId) {
        $mysql = new Mysql();
        $query = "SELECT x,y FROM imagesSizes WHERE id = $sizeId";
        $size = $mysql->getRow($query);
        $this->imageSizeX = $size['x'];
        $this->imageSizeY = $size['y'];
    }

    private function imageResize($forcedwidth, $forcedheight, $sourcefile, $destfile, $ext){
        $fw = $forcedwidth;
        $fh = $forcedheight;
        $is = getimagesize( $sourcefile );
        if( $is[0] >= $is[1]){
            $orientation = 0;
        }
        else{
            $orientation = 1;
            $fw = $forcedheight;
            $fh = $forcedwidth;
        }
        if ( $is[0] > $fw || $is[1] > $fh ){
            if( ( $is[0] - $fw ) >= ( $is[1] - $fh)){
                $iw = $fw;
                $ih = ( $fw / $is[0] ) * $is[1];
            }
            else{
                $ih = $fh;
                $iw = ( $ih / $is[1] ) * $is[0];
            }
            $t = 1;
        }
        else{
            $iw = $is[0];
            $ih = $is[1];
            $t = 2;
        }
        if ($t==1){
            $img_dst = imagecreatetruecolor( $iw, $ih );
            if($ext == 'png' OR $ext=='gif'){
                imagealphablending($img_dst, false);
                imagesavealpha($img_dst,true);
                $transparent = imagecolorallocatealpha($img_dst, 255, 255, 255, 127);
                imagefilledrectangle($img_dst, 0, 0, $iw, $ih, $transparent);
            }
            switch($ext){
                case 'gif': $img_src = imagecreatefromgif($sourcefile); break;
                case 'jpeg':
                case 'jpg': $img_src = imagecreatefromjpeg($sourcefile);  break;
                case 'png': $img_src = imagecreatefrompng($sourcefile); break;
                default:  trigger_error('Unsupported filetype!', E_USER_WARNING);  break;
            }
            imagecopyresampled( $img_dst, $img_src, 0, 0, 0, 0, $iw, $ih, $is[0], $is[1] );
            switch ($ext) {
                case 'gif': imagegif($img_dst,$destfile); break;
                case 'jpeg':
                case 'jpg': imagejpeg($img_dst,$destfile);  break;
                case 'png': imagepng($img_dst,$destfile); break;
                default:  trigger_error('Failed resize image!', E_USER_WARNING);  break;
            }
        }
        else if ($t==2){
            copy( $sourcefile, $destfile);
        }
    }

    public function rotate(){
        // zwrócenie realnych rozmiarów zdjęcia na wypadek jakby żądane wymiary były większe niż wymiary zdjęcia źródłowego
        $imageName = $this->file['name'];
        $ext = $this->file['ext'];
        $sizes = array('s','b');

        foreach($sizes as $size){
            $path = "$this->source/$imageName"."_$size.".$ext;
            if(file_exists($path)){

                $imageinfo=getimagesize($path);
                switch($imageinfo['mime']){
                    case "image/jpg":
                    case "image/jpeg":
                    case "image/pjpeg": $src_img=imagecreatefromjpeg("$path");
                                        break;
                    case "image/gif":   $src_img = imagecreatefromgif("$path");
                                        break;
                    case "image/png":
                    case "image/x-png": $src_img = imagecreatefrompng("$path");
                                        break;
                }
                $src_img = imagerotate($src_img, -90, 0);
                imagejpeg ($src_img,$path);

                $newSize[$size] = getimagesize($path);
            }
        }

        //losowy int na jako parametr w celu uniknięcia wyświetlenia zdjęcia z cache'u
        $random = rand(1, 1000);
        $this->file['ext'] .= "?$random";
        $this->file['size'] = $newSize;
        $this->file['path'] = "$this->source";
    }
}
?>